package controller;
import Pojo.QueryHouseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import Pojo.Houselist;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import service.HouselistService;
import cn.itcast.utils.Page;
import Pojo.QueryVo;

import javax.servlet.http.HttpServletRequest;

@Controller

public class HoustlistController {
	@Autowired
	private HouselistService houselistService;
	
//	@RequestMapping("/houselist")
	public String houselist(Model model ,@RequestParam(required=false,defaultValue="1") Integer page,
            @RequestParam(required=false,defaultValue="6") Integer pageSize){
		
		 PageHelper.startPage(page, pageSize);
		List<Houselist> houselist=houselistService.selectAll();
	PageInfo<Houselist> p=new PageInfo<Houselist>(houselist);
		
				
		model.addAttribute("p", p);
		model.addAttribute("houselist",houselist);
		model.addAttribute("mainPage","houselist.jsp");
		return "zuke/main";
	}
//	@RequestMapping("/select")
	@RequestMapping("/houselist")
	public String select(Model model, QueryHouseVo vo, @RequestParam(required=false,defaultValue="1") Integer page,
							@RequestParam(required=false,defaultValue="6") Integer pageSize){
		PageHelper.startPage(page, pageSize);
		List<Houselist> list=houselistService.select(vo);
		PageInfo<Houselist> p=new PageInfo<Houselist>(list);
		model.addAttribute("houselist", list);
		model.addAttribute("p", p);
		model.addAttribute("mainPage","houselist.jsp");
		model.addAttribute("vo", vo);
		return "zuke/main";
	}
//	@RequestMapping("/ahouselist")
	public String ahouselist(Model model ,@RequestParam(required=false,defaultValue="1") Integer page,
            @RequestParam(required=false,defaultValue="6") Integer pageSize){
		
		 PageHelper.startPage(page, pageSize);
		List<Houselist> houselist=houselistService.selectAll();
	PageInfo<Houselist> p=new PageInfo<Houselist>(houselist);
		
				
		model.addAttribute("p", p);
		model.addAttribute("houselist",houselist);
		model.addAttribute("mainPage","ahouselist.jsp");
		return "admin/main1";
	}
//	@RequestMapping("/selectAdmin")
	@RequestMapping("/ahouselist")
	public String selectAdmin(Model model, QueryHouseVo vo, @RequestParam(required=false,defaultValue="1") Integer page,
							@RequestParam(required=false,defaultValue="6") Integer pageSize){
		PageHelper.startPage(page, pageSize);
		List<Houselist> list=houselistService.select(vo);
		PageInfo<Houselist> p=new PageInfo<Houselist>(list);
		model.addAttribute("houselist", list);
		model.addAttribute("p", p);
		model.addAttribute("mainPage", "ahouselist.jsp");
		model.addAttribute("vo", vo);
		return "admin/main1";
	}

	@RequestMapping("/addhouse")
	public String addhouse(HttpServletRequest request, Integer id, String houseid,
						   String address,
						   double area,
						   double price,
						   String status,
						   MultipartFile img, Model model){
		Houselist houselist = new Houselist();
		houselist.setId(id);
		houselist.setAddress(address);
		houselist.setArea(area);
		houselist.setHouseid(houseid);
		houselist.setPrice(price);
		houselist.setStatus(status);


		//上传文件路径
		String path = request.getSession().getServletContext().getRealPath("/images/");
		if(!img.isEmpty()) {
			//上传文件名
			String filename = img.getOriginalFilename();
			filename = UUID.randomUUID() + filename;
			File filepath = new File(path,filename);
			//判断路径是否存在，如果不存在就创建一个
			if (!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			//将上传文件保存到一个目标文件当中
			try {
				img.transferTo(new File(path + File.separator + filename));
				System.out.println("文件名:"+filename);
				houselist.setImg(filename);
			} catch (IOException e) {
				System.out.println("上传图片错误"+e);
			}
		}
		houselistService.inserthouse(houselist);
		model.addAttribute("mainPage", "addhouse.jsp");
		return "admin/main1";
	}

	@RequestMapping("/toaddhouse")
	public String toaddhoust(Model model){
		model.addAttribute("mainPage","addhouse.jsp");
		
		return "admin/main1";
	}
	@RequestMapping("/deletehouse")
	public String deletehouse(Integer id){
		houselistService.deletehouse(id);
		
		
		return "redirect:ahouselist.action";
	}
	@RequestMapping("/toahouselist")
	public String toahouselist(){
		
		
		
		return "ahouselist.action";
	}
	@RequestMapping("/findid")
	public String findid(Integer id,Model model){
		Houselist list=houselistService.findid(id);
		model.addAttribute("houselist",list);
		model.addAttribute("mainPage", "changehouse.jsp");
		return "admin/main1";
	}
	@RequestMapping(value = "/findhouseidupdate", method = RequestMethod.POST)
	public String findhouseidupdate(HttpServletRequest request, Integer id, String houseid,
									String address,
									double area,
									double price,
									String status,
									MultipartFile img, Model model){
		Houselist houselist = new Houselist();
		houselist.setId(id);
		houselist.setAddress(address);
		houselist.setArea(area);
		houselist.setHouseid(houseid);
		houselist.setPrice(price);
		houselist.setStatus(status);

		//上传文件路径
		String path = request.getSession().getServletContext().getRealPath("/images/");
		if(!img.isEmpty()) {
			//上传文件名
			String filename = img.getOriginalFilename();
			filename = UUID.randomUUID() + filename;
			File filepath = new File(path,filename);
			//判断路径是否存在，如果不存在就创建一个
			if (!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			//将上传文件保存到一个目标文件当中
			try {
				img.transferTo(new File(path + File.separator + filename));
				System.out.println("文件名:"+filename);
				houselist.setImg(filename);
			} catch (IOException e) {
				System.out.println("上传图片错误"+e);
			}
		}

		Houselist list=houselistService.findhouseidupdate(houselist);
		if(list!=null){
			model.addAttribute("houselist",houselist);
			model.addAttribute("path",path);
			model.addAttribute("mainPage", "changehouse.jsp");
			model.addAttribute("error","该房屋id已存在");
			return "admin/main1";
		}
		else{
			if ("".equals(houselist.getImg()) || houselist.getImg() == null) {
				houselistService.updatehouse2(houselist);
			}else{
				houselistService.updatehouse(houselist);
			}
			model.addAttribute("houselist",houselist);
			model.addAttribute("path",path);
			model.addAttribute("mainPage", "changehouse.jsp");
			model.addAttribute("error","更新成功");
			return "admin/main1";
		}
	}
	
}
